package com.alex.managerlogin.controller.admin;

import com.alex.server.dto.LoginUserDto;
import com.alex.server.dto.UserDto;
import com.alex.server.dto.PageDto;
import com.alex.server.dto.ResponseDto;
import com.alex.server.service.IUserService;
import com.alex.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;

@RestController
@RequestMapping("/admin/user")
public class UserController {

    private static final Logger LOG = LoggerFactory.getLogger(UserController.class);
    public static final String BUSINESS_NAME = "用户信息主体表";

    @Autowired
    private IUserService userService;

    /**
     * 列表查询
     */
    @PostMapping("/list")
    public ResponseDto list(@RequestBody PageDto pageDto) {
        ResponseDto responseDto = new ResponseDto();
        userService.list(pageDto);
        responseDto.setContent(pageDto);
        return responseDto;
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public ResponseDto save(@RequestBody UserDto userDto) {
        // 保存校验
        ValidatorUtil.length(userDto.getRelationId(), "组织id（is_admin字段为1时，此字段为必填）", 1, 25);
        ValidatorUtil.require(userDto.getUsername(), "用户名");
        ValidatorUtil.length(userDto.getUsername(), "用户名", 1, 150);
        ValidatorUtil.length(userDto.getUserEmail(), "邮箱", 1, 60);
        ValidatorUtil.require(userDto.getIsEnabled(), "是否启用 true是   false否");
        ValidatorUtil.length(userDto.getLastLoginIp(), "最后登录IP", 1, 50);
        ValidatorUtil.require(userDto.getPassword(), "密码");
        ValidatorUtil.length(userDto.getPassword(), "密码", 1, 50);
        ValidatorUtil.require(userDto.getLoginErrorCount(), "登录错误次数");
        ValidatorUtil.length(userDto.getUsePhone(), "手机号", 1, 11);
        ValidatorUtil.require(userDto.getIsAdmin(), "是否管理员");
        ValidatorUtil.require(userDto.getIsResetPassword(), "是否已重置密码");
        ValidatorUtil.require(userDto.getPassMsgHasSend(), "提醒修改密码消息是否已发");
        ValidatorUtil.require(userDto.getCreateTime(), "创建时间");
        ValidatorUtil.require(userDto.getDeletedFlag(), "逻辑删除标识。仅且仅有0和1两个值，1表示已经被逻辑删除，0表示正常可用。");

        ResponseDto responseDto = new ResponseDto();
        userService.save(userDto);
        responseDto.setContent(userDto);
        return responseDto;
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        ResponseDto responseDto = new ResponseDto();
        userService.delete(id);
        return responseDto;
    }

    @PostMapping("/managerLogin")
    public ResponseDto managerLogin(@RequestBody LoginUserDto userDto, HttpServletRequest request, HttpServletResponse response) throws IOException, NoSuchAlgorithmException {

        ResponseDto responseDto = new ResponseDto();

        String respStr = userService.loginSuccess(userDto, request, response);

        if (respStr.length() > 10) {
            responseDto.setMessage("登录成功");
            responseDto.setCode("200");
            responseDto.setContent(respStr);
        } else {
            responseDto.setMessage("登录失败");
            responseDto.setCode("10001");
            responseDto.setContent(respStr);
            responseDto.setSuccess(false);
        }

        return responseDto;
    }
}
